package com.baozun.houji.order.viewmodel

import android.text.Editable
import androidx.lifecycle.MutableLiveData
import com.baozun.houji.order.model.bean.RefundInfoBean
import com.baozun.houji.order.network.orderApiService
import com.baozun.houji.order.R
import com.baozun.houji.order.model.bean.RefundReasonBean
import com.baozun.houji.order.model.enums.RefundType
import com.baozun.houji.order.model.request.RefundReqBean
import com.blankj.utilcode.util.ToastUtils
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.request

/**
 * @Title：
 * @Description：
 * @Author：xing.luo
 * @Since：2022/11/20
 * @Version：1.0.0
 */
class ApplyRefundViewModel : BaseViewModel() {
    var mReturnGoodsReasonList = mutableListOf<RefundReasonBean>()
    var mRefundReasonList = mutableListOf<RefundReasonBean>()

    var mRefundReason = MutableLiveData<RefundReasonBean>()
    var refundInfoBean = MutableLiveData<RefundInfoBean>()
    var mRefundType = MutableLiveData<RefundType>()
    var isOnlyReturnMoney = MutableLiveData<Boolean>()
    var mRefundDesc = MutableLiveData<String>()
    var mProblemDescCount = MutableLiveData("0/200")

    /**
     * 获取退款信息
     *
     */
    fun getRefundInfo(orderNo: String) {
        request({ orderApiService.getRefundInfo(orderNo) }, { it ->
            refundInfoBean.value = it
        }, isShowDialog = false)
    }

    /**
     * 获取退款原因
     * 售后类型 1 仅退款 2 退货退款
     */
    fun getRefundReason(refundType: Int, reasonList: (MutableList<RefundReasonBean>) -> Unit) {
        if (refundType == RefundType.REFUND.type && mRefundReasonList.isNotEmpty()) {
            reasonList.invoke(mRefundReasonList)
            return
        } else if (refundType == RefundType.REFUND_AND_RETURN_GOODS.type && mReturnGoodsReasonList.isNotEmpty()) {
            reasonList.invoke(mReturnGoodsReasonList)
            return
        }
        request({ orderApiService.getRefundReason(refundType) }, {
            if (refundType == RefundType.REFUND.type) {
                mRefundReasonList = it
            } else {
                mReturnGoodsReasonList = it
            }
            reasonList.invoke(it)
        })
    }

    fun onTextChange(editable: Editable?) {
        mProblemDescCount.value = "${editable.toString().length}/200"
    }

    /**
     * 提交申请
     */
    fun submit(orderNo: String, successCallBack: (refundId: String) -> Unit) {
        checkParams {
            request({ orderApiService.applyRefund(RefundReqBean(orderNo, mRefundType.value?.type, mRefundReason.value?.id, mRefundDesc.value)) }, {
                successCallBack.invoke(it)
                ToastUtils.showShort(com.baozhun.mall.res.R.string.submit_success)
            }, isShowDialog = true)
        }
    }

    private fun checkParams(checkSuccessCallBack: () -> Unit) {
        if (mRefundType.value == null) {
            ToastUtils.showShort(com.baozhun.mall.res.R.string.please_choose_apply_service)
            return
        } else if (mRefundReason.value == null) {
            ToastUtils.showShort(com.baozhun.mall.res.R.string.please_choose_refund_reason)
            return
        }
        checkSuccessCallBack.invoke()
    }
}